# -*- coding: utf-8 -*-
__author__ = 'yanshi'
#http://www.youdaili.cn/Daili/
import MySQLdb
from com.umessage.config import setting
from com.umessage.bean import proxy_site
class DBUtils(object):

    def getConnection(self):
        return MySQLdb.connect(host=setting.database['host'],port=setting.database['port'],user=setting.database['user'],passwd=setting.database['passwd'],db=setting.database['db'],charset=setting.database['charset'])

    def load_proxy_site(self):
        proxySites=[]
        con = self.getConnection()
        cursor = con.cursor()
        cursor.execute("select id,url,template from proxy_site where enable='Y'")
        result = cursor.fetchall()
        for data in result:
            site = proxy_site.ProxySite(id=data[0],url=data[1],template=data[2])
            proxySites.append(site)
        con.close()
        return proxySites

    def insert_proxy_page_url(self,url,template,domian,update_time):
        con = self.getConnection()
        cursor = con.cursor()
        cursor.execute('insert into proxy_page (url,template,domian,update_time) values (%s,%s,%s,%s)'%(url,template,domain,update_time))
        con.commit()
        con.close()

    def load_proxy_page(self):
        proxyPages=[]
        con = self.getConnection()
        cursor = con.cursor()
        cursor.execute("select id,template,updateTime,createTime from proxy_page where is_fetch='N'")
        result = cursor.fetchall()
        for data in result :
            page = proxy_site.ProxyPage(id=data[0],template=data[1],updateTime=data[2],createTime=data[3])
            proxyPages.append(page)
        con.close()
        return proxyPages

    def insert_proxy_ip(self,ip,port,ms):
        con = self.getConnection()
        cursor = con.cursor()
        cursor.execute("insert into proxy_ip (ip,port,ms) values ('%s',%s,0)"%(ip,port,ms))
        con.commit()

if __name__ == '__main__':
    urls = DBUtils().load_proxy_site()
    for url in urls :
        pages = url.loadPage()
        print len(pages)
        for page in pages :
            proxyPage=proxy_site.ProxyPage(1,page[0],'ip_temp',page[1],'2013-05-11')
            proxys =proxyPage.loadProxyInfo()
            print len(proxys)
            for proxy in proxys:
                DBUtils().insert_proxy_ip(proxy[0],proxy[1])







